<template>
  <div class="search-input">
    <form action="/">
      <van-search
        ref="input"
        v-model="searchValue"
        :show-action="scan"
        class="search-value"
        :placeholder="placeholder"
        clearable
        @search="searchHandle"
        @input="inputHandle"
        @focus="focusHandle"
        @blur="blurHandle"
        @clear="clearHandle"
        @cancel="searchHandle"
      >
        <template #action>
          <svg-icon v-if="scan" class="icon-grey" icon-class="a-saomiaoerweimasaoyisao-line" @click.native.stop="scanHandle" />
        </template>
      </van-search>
    </form>
  </div>
</template>
<script>
export default {
  name: 'SearchInput',
  inheritAttrs: true,
  props: {
    placeholder: {
      type: String,
      default: '请输入关键词搜索'
    },
    value: {
      type: [String, Number],
      default: ''
    },
    scan: {
      type: Boolean,
      default: false
    },
    focus: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      searchValue: ''
    }
  },
  computed: {
    showClear() {
      return this.$isDef(this.searchValue) && !this.$isEmpty(this.searchValue)
    }
  },
  watch: {
    value: {
      handler(nv, old) {
        this.searchValue = nv
      },
      immediate: true
    }
  },
  mounted() {
    if (this.focus) {
      this.getFocus()
    }
  },
  methods: {
    clearHandle() {
      this.searchValue = ''
      this.$emit('input', this.searchValue)
      this.$emit('clear')
    },

    inputHandle(value) {
      this.$emit('input', `${value || ''}`.trim())
    },
    focusHandle(e) {
      this.$emit('focus')
    },
    blurHandle(e) {
      const value = e.target.value
      this.$emit('blur', `${value || ''}`.trim())
    },
    searchHandle() {
      const value = this.searchValue
      this.$emit('search', `${value || ''}`.trim())
    },
    scanHandle() {
      this.$nativeHandler('scanQRCode').then(res => {
        this.$emit('scan', this.searchValue)
      }).catch(err => {
        console.log(err)
      })
    },
    getFocus() {
      this.$refs.input.querySelector('input').focus()
    },
    blur() {
      this.$refs.input.querySelector('input').blur()
    }
  }
}
</script>
<style lang="less">
@fontSize: 15.4px;
@iconColor: #BBBBBB;
.svgIcon(){
  font-size: @fontSize;
  color: @iconColor;
  flex: 0 0 auto;
}
.search-input{
  padding: 7px 10px;
  height: 30px;
  border-radius: 15px;
  background-color: #FFFFFF;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
  .search{
    .svgIcon();
    font-weight: 600;
  }
  .search-value{
    flex: 1;
    padding: 0;
    height: 30px;
    overflow: hidden;
    &::placeholder{
      color: @iconColor;
    }
  }

  .van-search{
    .van-search__content{
      padding-left: 0;
      background: transparent;
    }
    .van-field__left-icon{
      color: #999;
    }
    .van-cell{
      padding-top:0 ;
      padding-bottom: 0;
      height: 30px;
      line-height: 30px;
    }
    .van-search__action{
      margin-right: 10px;
    }
  }
  .clear{
    .svgIcon()
  }
  .scan{
    .svgIcon()
  }
}
</style>
